/** \file    ActivateHalfCoveredSegments.cpp
  * \brief   Activates (sets flag) segments covered at least by a pixel (alternatively at least on half) by a given bitmap
  * \date    2001 - 2003
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/segm/src/ActivateHalfCoveredSegments.cpp,v 1.1 2004/05/20 09:11:46 cscom1r4 Exp $
  */

#include "segm/Segmentation.h"
#include "imglib/bitmap.h"

void Segmentation::covering(const img::bitmap & bmp, DoubleArr & cover) const
{
	assert(width() == bmp.width() && height() == bmp.height());
	cover.clear();
	cover.resize(count(), 0);
	int w = width();
	int h = height();
	int x,y;
	for( y = 0; y != h; ++y ) {
		for( x = 0; x != w; ++x ) {
			if( !bmp.at(x,y) ) continue;
			int si = segmentIndex(x,y);
			if( si == -1 ) continue;
			++cover[si];
		}
	}
	for( int i = 0; i != count(); ++i ) {
		cover[i] /= segmentByIndex(i).pixelCount;
	}
}

void Segmentation::activateCoveredSegments(const img::bitmap & bmp, 
	double coveringRatio)	///< (0.0 means covering more than by a pixel)
{
	DoubleArr cover;
	covering(bmp, cover);

	///activation
	for( int i = 0; i != count(); ++i ) {
		if( cover[i] > coveringRatio ) segmentByIndex(i).active = true;
	}
}

void Segmentation::deactivateCoveredSegments(const img::bitmap & bmp, 
	double coveringRatio)	///< (1.0 means not-covering more than a pixel)
{
	DoubleArr cover;
	covering(bmp, cover);

	///activation
	for( int i = 0; i != count(); ++i ) {
		if( cover[i] < coveringRatio ) segmentByIndex(i).active = false;
	}
}
